<?php
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['current_room'])) {
    $_SESSION['current_room'] = 1;
    $_SESSION['inventory'] = [];
}

// 定义密室场景
$rooms = [
    1 => [
        'description' => '你身处一个昏暗的房间，四周摆放着陈旧的书架，墙上挂着一幅奇怪的画。',
        'items' => ['书架', '画'],
        'clues' => [
            '书架' => '你在书架上发现一本破旧的日记，日记里似乎记录着一些重要信息。',
            '画' => '这幅画看起来可以移动，后面好像藏着什么东西。'
        ],
        'actions' => [
            '查看书架' => function () {
                global $_SESSION;
                if (!in_array('日记', $_SESSION['inventory'])) {
                    $_SESSION['inventory'][] = '日记';
                    return '你从书架上拿到了一本日记。';
                } else {
                    return '书架上已经没有东西了。';
                }
            },
            '移动画' => function () {
                global $_SESSION;
                if (!in_array('钥匙', $_SESSION['inventory'])) {
                    $_SESSION['inventory'][] = '钥匙';
                    return '你移动了画，发现后面藏着一把钥匙。';
                } else {
                    return '画后面已经没有东西了。';
                }
            }
        ]
    ],
    2 => [
        'description' => '这是一个狭窄的走廊，尽头有一扇紧闭的门。',
        'items' => ['门'],
        'clues' => [
            '门' => '这扇门需要一把钥匙才能打开。'
        ],
        'actions' => [
            '使用钥匙开门' => function () {
                global $_SESSION;
                if (in_array('钥匙', $_SESSION['inventory'])) {
                    unset($_SESSION['inventory'][array_search('钥匙', $_SESSION['inventory'])]);
                    return '你用钥匙打开了门，成功逃脱密室！游戏结束。';
                } else {
                    return '你没有钥匙，无法打开这扇门。';
                }
            }
        ]
    ]
];

// 处理用户输入
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $action = $_POST["action"];
    $current_room = $_SESSION['current_room'];

    if (isset($rooms[$current_room]['actions'][$action])) {
        $result = $rooms[$current_room]['actions'][$action]();
        if (strpos($result, '成功逃脱密室！')!== false) {
            // 游戏结束，重置会话
            session_destroy();
        }
    } else {
        $result = '无效的指令，请重新输入。';
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>密室逃脱游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            background-color: #f4f4f9;
        }

        h1 {
            color: #333;
        }

        #game-info {
            margin: 20px;
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        #inventory {
            margin: 20px;
        }

        form {
            margin-top: 20px;
        }

        input[type="text"] {
            padding: 8px;
            font-size: 16px;
            width: 300px;
        }

        input[type="submit"] {
            padding: 8px 16px;
            font-size: 16px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }

        #result {
            margin-top: 20px;
            color: green;
        }
    </style>
</head>

<body>
    <h1>密室逃脱游戏</h1>
    <div id="game-info">
        <p><?php echo $rooms[$_SESSION['current_room']]['description']; ?></p>
        <p>可交互物品：<?php echo implode(', ', $rooms[$_SESSION['current_room']]['items']); ?></p>
    </div>
    <div id="inventory">
        <p>你的物品栏：<?php echo empty($_SESSION['inventory']) ? '无' : implode(', ', $_SESSION['inventory']); ?></p>
    </div>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        <input type="text" name="action" placeholder="输入指令（如：查看书架）" required>
        <input type="submit" value="执行">
    </form>
    <?php if (isset($result)): ?>
        <div id="result"><?php echo $result; ?></div>
    <?php endif; ?>
</body>

</html>